<h1>Results for 
<?php
	$studentid = $this->session->userdata("userid");
	$subjectid = $this->input->post("subject");
	$this->db->select("subjectname");
	$this->db->from("subjects");
	$this->db->where("subjectid", $subjectid);
	$query = $this->db->get();
	foreach($query->result() as $subjectname){
		echo $subjectname->subjectname;
	}
?>
:</h1>
<?php
	$sched_constraints = array(
	                           "subjectid" => $subjectid,
	                           "adminstatus" => "approved",
							   "studentid" => NULL
	                          );
	
	//Generate an array whose schedules are approved for this subject.
	$this->db->where("subjectid", $subjectid);
	$this->db->where("adminstatus", "approved");
	$this->db->where("studentid", null);
	$approvedsched_query = $this->db->from("schedule");
	$count = $this->db->count_all_results();
	
	if($count == 0){
		echo "<p>Your search returned no results. Probable causes are:";
		echo "<ul>
		          <li>No teacher is offering this course yet.</li>
		          <li>Admins haven't approved schedules for this course yet.</li>
		      </ul>";
		echo "<p>Please try again later</p>";
	} else{
		$approvedsched_query = $this->db->get_where("schedule", $sched_constraints);
		foreach($approvedsched_query->result() as $row){
			$teacher_constraints = array(
			                             "userid" => $row->teacherid
			                            );
			$teacher_query = $this->db->get_where("teachers", $teacher_constraints);
			
			foreach($teacher_query->result() as $teacher_row){
				$teachername = $teacher_row->lastname . ", " . $teacher_row->firstname;
				$key = "" . $teacher_row->userid;
				$time = $row->date . ", " . $row->time;
				$d = $row->date;
				$t = str_replace(":", "-", $row->time);
				$schedindex = 0;
				$schedsavailable = count($time);
				
				while($schedindex < $schedsavailable){
					echo "<a href='reserve/" . $row->teacherid . "/$d/$t/$studentid'>$teachername - $time</a>";
					echo "<br />";
					$schedindex++;
				}
			}
		}
	}
?>
